using System;
using System.Collections.Generic;
using System.Text;

namespace Com.Guocz.Media.FileSystem
{
    class Program
    {
        public static void Main(string[] args)
        {
            //testChunkManager();
            testFS();
            //testCrypt();
            //testDataChunk();
        }

        private static void testDataChunk()
        {
            DataManager dataManager = new DataManager(getMeta(@"e:\bineon"));
            for (int i = 0; i < 100000; i++)
            {
                byte[] buf = new byte[900];

                new Random().NextBytes(buf);
                BlockMetaInfo block = dataManager.write(buf);
                byte[] tmp = dataManager.read(block);
                Console.WriteLine(buf.Length + "->" + tmp.Length + "@" + block);
                for (int j = 0; j < buf.Length; j++)
                {
                    if (buf[j] != tmp[j])
                    {
                        Console.WriteLine("error");
                    }
                }
                Console.WriteLine("---->" + i);
            }
        }

        private static void testCrypt()
        {
            Crypt.RijndaelCrypt cryptor = new Com.Guocz.Media.FileSystem.Crypt.RijndaelCrypt("heh", "haha");

            for (int i = 0; i < 100; i++)
            {
                byte[] buf = new byte[new Random().Next(1000) + 1];

                new Random().NextBytes(buf);
                byte[] tmp = cryptor.Encrypt(buf);
                byte[] def = cryptor.Decrypt(tmp);
                for (int j = 0; j < buf.Length; j++)
                {
                    if (buf[j] != def[j])
                    {
                        Console.WriteLine("error");
                    }
                }
                Console.WriteLine("---->" + i);
            }
        }

        private static void testFS()
        {
            FileSystem fs = null;
            try
            {
                fs = FileSystem.Open("hehe", "haha", getMeta(@"e:\bineon"));
            }
            catch
            {
                fs = FileSystem.Create("hehe", "haha", getMeta(@"e:\bineon"));
            }
            Path root = fs.Root;
            OutputStream stream = fs.create("/haha/haha");
            byte[] haha = new byte[] { 19, 84, 225 };
            stream.Write(haha, 0, haha.Length);
            stream.Close();
            foreach (var P in root.Children)
            {
                Console.WriteLine(P.Name);
            }
            InputStream instream = fs.open("/haha/haha");
            byte[] xx = new byte[8192];
            int len = instream.Read(xx, 0, xx.Length);
            instream.Close();
            Console.WriteLine(xx[0] + " " + xx[1] + " " + xx[2]);
        }
        static MetaInfo getMeta(string prefix)
        {
            MetaInfo meta = new MetaInfo();
            meta.IndexFile = prefix + ".idx";
            meta.DataFemale = prefix + ".fm";
            meta.DataMale = prefix + ".ml";
            return meta;
        }

        private static void testChunkManager()
        {
            ChunkManager chunk = ChunkManager.GetChunkManager(@"e:\bineon.dat");
            for (int i = 0; i < 100000; i++)
            {
                byte[] buf = new byte[1000];

                new Random().NextBytes(buf);
                int block = chunk.write(buf);
                byte[] tmp = chunk.read(block);
                Console.WriteLine(buf.Length + "->" + tmp.Length + "@" + block);
                for (int j = 0; j < buf.Length; j++)
                {
                    if (buf[j] != tmp[j])
                    {
                        Console.WriteLine("error");
                    }
                }
                Console.WriteLine("---->" + i);
            }
        }
    }
}
